< previous page page_23 next page >

Page 23
needed to keep track of more than one outstanding order? Would you add two, four, or six more order fields? You can see the problem.
Instead, you can add an additional table (the Orders Table) that can contain as many outstanding orders for a single customer as you need. After you create the Orders Table, you can establish a relationship between the Customer Table and the Orders Table using the LastName field (refer back to Figure 2.4). The LastName field is used as a pointer into the Orders Table to locate all the orders for this customer.
You can use many different approaches to establish relationships between tables. They are usually established through a key field. Key fields are covered in depth in the next section.
Key Fields
Usually, at least one data field in each data table acts as a key field for the table. Key fields in relational databases are used to define and maintain database integrity and to establish relationships between data tables. You create keys in your data table by designating one (field) or more in your table as either a primary key or a foreign key. A data table can have only one primary key, but it can have several foreign keys. The primary key is used to control the order in which the data is displayed. The foreign key is used to relate fields to fields in other (foreign) tables in the database.
Note.gif
Key fields are sometimes referred to as index fields or indexes. Both "key fields" and "index fields" are used interchangeably throughout the book. It is important to note that in most PC databases (Xbase, Paradox, Btreive, and so forth), indexes are used only to speed processing of large files and play only a minor role in maintaining table relationships. The Visual Basic database model (.mdb files) and other true relational database models use key fields to establish database integrity rules as well as to speed database search and retrieval.

As mentioned earlier, a data table can have only one primary key. The primary key is used to define a unique record in the data table. In the Customer table, the LastName field is the primary key field for the data table. This means that no two records in that table can have exactly the same value in the LastName fields (see Figure 2.7). Any attempt to add more than one record with an identical primary key would result in a database error.

 
< previous page page_23 next page >

If you like this book, buy it!